package com.danciyixue.boot.client.userCurrentUnit.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.core.security.util.SecurityUtils;
import com.danciyixue.boot.dcyx.model.form.UserCurrentUnitsForm;
import com.danciyixue.boot.dcyx.model.query.UserCurrentUnitsQuery;
import com.danciyixue.boot.dcyx.model.vo.UserCurrentUnitsVO;
import com.danciyixue.boot.dcyx.service.UserCurrentUnitsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户当前单元控制器
 */
@Tag(name = "10. 选中单元", description = "选中单元")
@RestController
@RequestMapping("/api/client/v1/user-current-unit")
@RequiredArgsConstructor
public class DcyxUserCurrentUnitController {

    private final UserCurrentUnitsService userCurrentUnitsService;

    /**
     * 获取用户当前选中单元
     */
    @GetMapping("/current")
    @Operation(summary = "获取用户当前选中单元")
    public Result<UserCurrentUnitsVO> getCurrentUnit() {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        UserCurrentUnitsVO vo = userCurrentUnitsService.getCurrentUnitByUserId(userId);
        return Result.success(vo);
    }

    /**
     * 更新用户当前选中单元
     */
    @PostMapping("/current")
    @Operation(summary = "更新用户当前选中单元")
    public Result<Void> updateCurrentUnit(@RequestBody UserCurrentUnitsForm form) {
        // 获取当前登录用户ID并设置到表单
        Long userId = SecurityUtils.getUserId();
        form.setUserId(userId);
        boolean result = userCurrentUnitsService.updateCurrentUnit(form);
        return Result.judge(result);
    }

    @Operation(summary = "用户当前选择单元信息分页列表")
    @GetMapping("/page")
    public PageResult<UserCurrentUnitsVO> getUserCurrentUnitsPage(UserCurrentUnitsQuery queryParams ) {
        IPage<UserCurrentUnitsVO> result = userCurrentUnitsService.getUserCurrentUnitsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增用户当前选择单元信息")
    @PostMapping
    public Result<Void> saveUserCurrentUnits(@RequestBody @Valid UserCurrentUnitsForm formData ) {
        boolean result = userCurrentUnitsService.saveUserCurrentUnits(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户当前选择单元信息表单数据")
    @GetMapping("/{id}/form")
    public Result<UserCurrentUnitsForm> getUserCurrentUnitsForm(
            @Parameter(description = "用户当前选择单元信息ID") @PathVariable Long id
    ) {
        UserCurrentUnitsForm formData = userCurrentUnitsService.getUserCurrentUnitsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户当前选择单元信息")
    @PutMapping(value = "/{id}")
    public Result<Void> updateUserCurrentUnits(
            @Parameter(description = "用户当前选择单元信息ID") @PathVariable Long id,
            @RequestBody @Validated UserCurrentUnitsForm formData
    ) {
        boolean result = userCurrentUnitsService.updateUserCurrentUnits(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户当前选择单元信息")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteUserCurrentUnitss(
            @Parameter(description = "用户当前选择单元信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userCurrentUnitsService.deleteUserCurrentUnitss(ids);
        return Result.judge(result);
    }

    /**
     * 标记单元学习完成
     */
    @PostMapping("/complete")
    @Operation(summary = "标记单元学习完成")
    public Result<Void> completeUnit(@RequestParam Long unitId) {
        boolean result = userCurrentUnitsService.completeUnit(unitId);
        return Result.judge(result);
    }
}
